package cz.tomasdvorak.cars.pages.vehicle;

import cz.tomasdvorak.cars.auth.Auth;
import cz.tomasdvorak.cars.dto.User;
import cz.tomasdvorak.cars.dto.Vehicle;
import cz.tomasdvorak.cars.pages.BasePage;
import cz.tomasdvorak.cars.utils.Lists;
import cz.tomasdvorak.cars.wicket.components.breadcrumbs.BreadcrumbItem;
import cz.tomasdvorak.cars.wicket.components.input.LabelForInput;
import org.apache.wicket.PageParameters;
import org.apache.wicket.authorization.strategies.role.annotations.AuthorizeInstantiation;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.Check;
import org.apache.wicket.markup.html.form.CheckGroup;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.model.ResourceModel;
import org.apache.wicket.model.util.CollectionModel;

import java.util.List;

@AuthorizeInstantiation(Auth.USER)
public class VehiclePermissionsPage extends BasePage {

    public VehiclePermissionsPage(PageParameters params) {
        this(params.getAsInteger("id"));
    }

    public VehiclePermissionsPage(int id) {
        Vehicle vehicle = getVehiclesDao().getById(id);
        CompoundPropertyModel<Vehicle> model = new CompoundPropertyModel<Vehicle>(vehicle);
        add(new permissionsForm("form", model));
        add(new Label("carName", vehicle.toString()));
    }

    protected List<BreadcrumbItem> getPageBreadcrumb() throws Exception {
        return Lists.arrayList(new BreadcrumbItem(new ResourceModel("breadcrumb.permissions")));
    }

    private final class permissionsForm extends Form<Vehicle> {

        private CheckGroup<Integer> allowedUsers;

        public permissionsForm(String id, IModel<Vehicle> vehicleIModel) {
            super(id, vehicleIModel);
            List<User> allUsers = VehiclePermissionsPage.this.getUserDao().getAllUSers();
            allowedUsers = new CheckGroup<Integer>("users",  new CollectionModel<Integer>());
            add(allowedUsers);
            allowedUsers.add(new ListView<User>("usersList", allUsers) {
                protected void populateItem(ListItem<User> listItem) {
                    final Check<Integer> check = new Check<Integer>("checkbox", new Model<Integer>(
                            listItem.getModelObject().getId()));
                    listItem.add(check);
                    listItem.add(new LabelForInput("name", listItem.getModelObject().getName(),
                            check));
                }
            });
            allowedUsers.setModelObject(vehicleIModel.getObject().getUsers());
            add(new Button("submit"));
        }

        @Override
        protected void onSubmit() {
            this.getModelObject().setUsers(allowedUsers.getModelObject());
            VehiclePermissionsPage.this.getVehiclesDao().insertOrUpdate(this.getModelObject());
            setResponsePage(VehiclePermissionsPage.class, new PageParameters("id=" + this.getModelObject().getId()));
        }
    }
}